package com.xuan.pmh.activity;

import java.util.Map;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.xuan.pmh.R;
import com.xuan.pmh.bean.User;
import com.xuan.pmh.util.ActivityUtil;
import com.xuan.pmh.util.FinalValue;
import com.xuan.pmh.util.ToastUtil;

/**
 * @author xuan 2014-10-30
 * 
 */
public class RegisterActivity extends HttpActivity {

	private EditText etUserId, etName, etPassword, etBirthday, etPhone;
	private Spinner spSex, spRole;
	private Button btRegister, btReturn;
	private User user = new User();

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_register;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		etUserId = (EditText) findViewById(R.id.etUserId);
		etName = (EditText) findViewById(R.id.etName);
		etPassword = (EditText) findViewById(R.id.etPassword);
		etBirthday = (EditText) findViewById(R.id.etBirthday);
		etPhone = (EditText) findViewById(R.id.etBirthday);
		spSex = (Spinner) findViewById(R.id.spSex);
		spRole = (Spinner) findViewById(R.id.spRole);
		btRegister = (Button) mFindViewAndSetOnClick(R.id.btRegister);
		btReturn = (Button) mFindViewAndSetOnClick(R.id.btReturn);
	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.btRegister:
			int role = ActivityUtil.getRole(spRole);
			user.role = role;
			user.userId = getEditTextString(etUserId);
			user.name = getEditTextString(etName);
			user.birthday = getEditTextString(etBirthday);
			user.phone = getEditTextString(etPhone);
			user.sex = getSex();
			paramsList.clear();
			paramsList.put("param", gson.toJson(user));
			paramsList.put("password", getEditTextString(etPassword));
			addPostRequest("register", paramsList, 0);
			sendPostRequest();
			break;
		case R.id.btReturn:
			finish();
			break;
		}
	}

	@Override
	public void afterPost(String result, int requestCode,
			Map<String, String> request) {
		// TODO Auto-generated method stub
		gsonBean = getGsonBean(result);
		if (gsonBean.state == FinalValue.SUCCESS) {
			Intent intent;
			switch (user.role) {
			case FinalValue.STUDENT:
				// intent = new
				// Intent(RegisterActivity.this,WritePaperActivity.class);
				break;
			case FinalValue.WORKER:
				break;
			case FinalValue.MANAGER:
				break;
			}
			ToastUtil.show(this, "申请成功,请等待审核");
		} else {
			ToastUtil.show(this, "注册失败,请填写正确");
		}
	}

	public String getSex() {
		String sex = spSex.getSelectedItem().toString();
		if (sex.equals("男")) {
			return "m";
		} else {
			return "f";
		}
	}

}
